package com.ruibo.modules.system.controller;

import com.ruibo.modules.system.criteria.DictTypeCriteria;
import com.ruibo.modules.system.service.SysDictTypeService;
import com.ruibo.modules.system.service.dto.SysDictTypeDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysDictType)表控制层
 *
 * @author sunfeng
 * @since 2020-05-21 15:33:50
 */
@RestController
@RequestMapping("/system/sysDictType")
public class SysDictTypeController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@el.check('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public ResponseEntity<SysDictTypeDto> getInfo(@PathVariable Long dictId) {
        return new ResponseEntity<>(sysDictTypeService.queryById(dictId), HttpStatus.OK);
    }


    @PreAuthorize("@el.check('system:dict:list')")
    @GetMapping("/list")
    public ResponseEntity<Object> list(DictTypeCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(sysDictTypeService.getAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@el.check('system:dict:add')")
    @PostMapping
    public ResponseEntity add(@Validated @RequestBody SysDictTypeDto sysDictType) {


        return new ResponseEntity<>(sysDictTypeService.insert(sysDictType), HttpStatus.OK);
    }


    /**
     * 修改字典类型
     */
    @PreAuthorize("@el.check('system:dict:edit')")
    @PutMapping
    public ResponseEntity changeStatus(@RequestBody SysDictTypeDto sysDictTypeDto) {

        return new ResponseEntity<>(sysDictTypeService.update(sysDictTypeDto), HttpStatus.OK);
    }

    /**
     * 删除字典
     */
    @PreAuthorize("@el.check('system:dict:remove')")
    @DeleteMapping("/{dictIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] dictIds) {
        sysDictTypeService.deleteById(dictIds);

        return new ResponseEntity<>(HttpStatus.OK);
    }
}
